package erwiki.ui.app.admin.handlers.workaround2;

import java.io.IOException;
import java.util.Collections;

import org.apache.log4j.Logger;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

import erwiki.api.IStorageCdo;
import erwiki.api.core.Engine;
import erwiki.api.event.PageEvent;
import jakarta.inject.Inject;

public class PagesImportHandler {

	protected static final Logger log = Logger.getLogger(PagesImportHandler.class);

	@Inject
	EventAdmin eventAdmin;

	@Inject
	Engine engine;

	@Execute
	public void execute(Shell shell) {
		IStorageCdo storageCdo = engine.getManager(IStorageCdo.class);
		try {
			storageCdo.loadAllContent();
			eventAdmin.sendEvent(new Event(PageEvent.Topic.REINDEX, Collections.emptyMap()));
		} catch (IOException e1) {
			MessageDialog.openError(shell, "Error pages import", e1.getMessage());
			log.error("Failed wiki pages import.", e1);
		}
	}

}
